#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : aimix
@File    : load_audio_materials.py
@IDE     : PyCharm
@Author  : admin
@Date    : 2025/4/26 18:18
"""
import json
import traceback
from PyQt5.QtCore import QThread, pyqtSignal
from utils.tools import request
from config import Config


class LoadAudioMaterialsWorker(QThread):
    """加载音频素材线程"""
    result = pyqtSignal(str)
    failed = pyqtSignal(Exception)

    def __init__(self, headers, parent=None):
        super(LoadAudioMaterialsWorker, self).__init__()
        self.headers = headers

    def run(self):
        try:
            payload = json.dumps({
                "type": "audio_synthesis"
            })
            url = f"{Config.Host}/api/get/synthesis/materials/"
            self.headers.update({'Content-Type': 'application/json'})
            response = request(url, method='POST', headers=self.headers, data=payload)
            response.raise_for_status()
            context = response.json()["data"]
            self.result.emit(json.dumps(context, ensure_ascii=False))
        except Exception as e:
            traceback.print_exc()
            self.failed.emit(e)